/*自动导出当前文件夹中的所有vue文件,添加到路由列表中 */
import i18n from "basic/i18n";
import components from "./components";
import routeInfos from "./elements";

export let routeNoAuth = [];
export let routeAuth = [];
//不需要登录的页面组件名,设置requireAuth为false
let ignoreNames = ["login", "register", "forgot-pwd"];
Object.keys(components).forEach(key => {
	let component = components[key];
	if (!ignoreNames.includes(key)) {
		let menu = routeInfos[key] ? routeInfos[key] : {};
		let routePath = menu.path ? menu.path : `/${key}`;
		let routeIcon = menu.icon ? menu.icon : "el-icon-s-data";
		let authRoute = {
			name: key,
			path: key === "menus" ? "/system/menus" : routePath,
			component: component,
			meta: {
				requireAuth: true,
				title: i18n.t(key),
				isKeepAlive: true,
				icon: key === "dashboard" ? "el-icon-s-home" : (key === "menus" ? "el-icon-menu" : routeIcon)
			}
		};
		routeAuth.push(authRoute);
	} else {
		let noAuthRoute = {
			name: key,
			path: `/${key}`,
			component: component,
			meta: {
				requireAuth: false,
				title: i18n.t(key),
				isKeepAlive: false,
				icon: ""
			}
		};
		routeNoAuth.push(noAuthRoute);
	}
});
let route404 = {
	path: "/404",
	name: "page-404",
	component: resolve => require(["components/public/Page404.vue"], resolve),
	meta: {
		requireAuth: false,
		title: i18n.t("page-404"),
		icon: "el-icon-warning"
	}
};
let route500 = {
	path: "/500",
	name: "page-500",
	component: resolve => require(["components/public/Page500.vue"], resolve),
	meta: {
		requireAuth: false,
		title: i18n.t("page-500"),
		icon: "el-icon-error"
	}
};
routeAuth.push(route404, route500);

export default {routeNoAuth, routeAuth};

// 注意点：
//
// 1、关于带参数的路由总结如下：
//
// 无论是直接路由“path" 还是命名路由“name”，带查询参数query，地址栏会变成“/url?查询参数名：查询参数值“;
// 直接路由“path" 带路由参数params params 不生效;
// 命名路由“name" 带路由参数params 地址栏保持是“/url/路由参数值”;
//
// 2、设置路由map里的path值：
//
// 带路由参数params时，路由map里的path应该写成:  path:'/apple/:color' ;
// 带查询参数query时，路由map里的path应该写成: path:'/apple' ；
//
// 3、获取参数方法：
//
// 在组件中：  {{$route.params.color}}
// 在js里： this.$route.params.color
